﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Windows Live Web Auth Control</title>
    <style type="text/css">
        body
        {
        	margin:0px;
        	padding:0px;
        	background-color:#5A6A7E;
        }
        #SignOut a:hover
        {
        	background-color:#2C486B;
        }
        #SignOut a
        {
        	text-align:center;
        	padding-top:5px;
        	font-size:14px;
        	background-color:#5A6A7E;
        	font-family:Verdana;
        	color:#E4F1FF;
        	text-decoration:none;
        	width:60px;
        	height:30px;
        	display:block;
        }
        #SignIn a
        {
        	text-align:center;
        	font-size:13px;
        	background-color:#5A6A7E;
        	color:#E4F1FF;
        	text-decoration:none;
        	width:60px;
        	height:30px;
        }
    </style>
  </head>
  
  <body id="ControlBody" onload="onload()" style="">
    <script type="text/javascript">
      var g_baseurl = "https://login.live.com/";
      var g_allowed_tags = [
        "font-family", 
        "font-weight", 
        "font-style",
        "font-size",
        "color",
        "background"
      ];

      var g_appid, g_context, g_style, g_alg; 
      
      function onload()
      {
        var query = getQuery();
        g_appid = sanitize(query["appid"]);
        if(!g_appid){
          alert("Windows Live Web Auth Control: Application ID parameter (appid) was not set.");
          return;
        }
        g_context = query["context"];
        g_style = makeStyleString(query["style"]);
        g_alg = (query["alg"] || "wsignin1.0");
        g_alg = escape(g_alg);
        display();
      }

      function display()
      {
//        if(g_style){
//          var body = document.getElementById("ControlBody");
//          var sil = document.getElementById("SignInLink");
//          var sol = document.getElementById("SignOutLink");
//          body.style.cssText = g_style;
//          sil.style.cssText = g_style;
//          sol.style.cssText = g_style;
//        }
        var si = document.getElementById("SignIn");
        var so = document.getElementById("SignOut");
        so.childNodes[0].href=getLogoutUrl();
        si.childNodes[0].href=getLoginUrl();
        if(isLoggedIn()){
          si.style.display = "none";
          so.style.display = "block";
        }
        else{
          si.style.display = "block";
          so.style.display = "none";
        }
      }

      function isLoggedIn()
      {
        var LOGINCOOKIE = "webauthtoken";

          var loginstate = getCookie(LOGINCOOKIE);
          if(loginstate)
          {
             return true;
          }
          else
          {
             return false;
          }
      }

      function getLoginUrl()
      {
        var login = g_baseurl;
        login += "wlogin.srf?appid=" + g_appid;
        login += "&alg=" + g_alg;
        if(g_context != null){
          login += "&appctx=" + g_context;
        }
        return login;
      }

      function getLogoutUrl()
      {
        var logout = g_baseurl;
        logout += "logout.srf?appid=" + g_appid;
        return logout;
      }

      function makeStyleString(style)
      {
        var output = "";
        if(!style)
          return;
        style = unescape(style).replace(/(\s|\+)/g, "");
        if(!style)
          return;

        var tags = style.split(";");
        var allowed_tags = new RegExp("^(" + g_allowed_tags.join("|") +  ")$");

        for(var i in tags){
          var tag = tags[i];
          if(!tag)
            continue;
          var t = tag.split(":");
          if(t.length < 2)
            continue;
          var k = t[0];
          var v = t[1];

          if(!k.match(allowed_tags)){
            alert("Windows Live Web Auth Control: Style tag not accepted: " + k);
            continue;
          }

          v = sanitize(v);
          if(!v){
            alert("Windows Live Web Auth Control: Style value not accepted for: " + k);
            continue;
          }

          output += k + ": " + v + "; ";
        }

        return output;
      }

      function sanitize(unsafe)
      {
        if(unsafe)
          return unsafe.match(/^[\w#\-]*$/);
        return;
      }

      function getQuery() 
      {
        var qs = window.location.search.substring(1);
        var pairs = qs.split("&");
        var query = [];
        for (var i = 0; i < pairs.length; i++) {
          var pair = pairs[i].split("=");
          if(pair.length < 2)
            continue;
          query[pair[0]] = pair[1];
        }
        return query;
      }

      function getCookie(name)
      {
        var cookies = document.cookie.split(';');
        for(var i = 0; i < cookies.length; i++){
          var cookie = cookies[i].replace(/^\s+/, '');
          if(cookie.indexOf(name + '=') == 0){
            return cookie.split("=")[1];
          }
        }
        return null;
      }
    </script>

    <div id="SignOut" style="display:none"><a id="SignOutLink" target="_top">退出</a></div>
    <div id="SignIn" style="display:none;"><a id="SignInLink" target="_top">登录网站</a></div>
  </body>
</html>
